package com.lenovo.wxtoken.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 系统异常时发送报警邮件
 */
@Service
public class EmailService {
    @Autowired
    JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String senderEmail;

    private String adminEmail = "845024839@qq.com";

    public void sendEmail(String text) throws MessagingException {
        MimeMessage mimeMessage = this.mailSender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
        message.setFrom(senderEmail);
        message.setTo(adminEmail);
        message.setSubject("企业微信基础服务故障");
        message.setText("<div style=\"color:red\">"+ text +"</div>", true);
        this.mailSender.send(mimeMessage);

    }
}
